(function(){

	// the minimum version of jQuery we want
	var v = "1.3.2";

	// check prior inclusion and version
	if (window.jQuery === undefined || window.jQuery.fn.jquery < v) {
		var done = false;
		var script = document.createElement("script");
		script.src = "http://ajax.googleapis.com/ajax/libs/jquery/" + v + "/jquery.min.js";
		script.onload = script.onreadystatechange = function(){
			if (!done && (!this.readyState || this.readyState == "loaded" || this.readyState == "complete")) {
				done = true;
				initMyBookmarklet();
			}
		};
		document.getElementsByTagName("head")[0].appendChild(script);
	} else {
		initMyBookmarklet();
	}
	
	function initMyBookmarklet() {
		(window.myBookmarklet = function() {
			 var source = {
				type: null,
				content: null
			};			
			var myurl = window.location.protocol + "//" + window.location.host + window.location.pathname;
			//var myurl = "http://mypage.apple.com";
			if (myurl.indexOf('mypage.apple.com') == -1){
				alert("Prueba de Open the bookmark when on mypage to save the selected calendar week.");
				}else{
			
				raw_source = document.documentElement.outerHTML;
				
				//Adapt the beginning of the date to match the php script of Byron
				raw_source = raw_source.replace(/a partir de/gi,"begins");
				
				//Change month names that doesn't match the english translation
				raw_source = raw_source.replace(/Ene/g,	"Jan");
				raw_source = raw_source.replace(/Abr/g,	"Apr");
				raw_source = raw_source.replace(/Ago/g,	"Aug");
				raw_source = raw_source.replace(/Dic/g,	"Dec");
				
				//Change names of the days to work with the php code:
				raw_source = raw_source.replace(/Lunes/g,	"Monday");
				raw_source = raw_source.replace(/Martes/g,	"Tuesday");
				raw_source = raw_source.replace(/Mi.*rcoles/g,"Wednesday");
				raw_source = raw_source.replace(/Jueves/g,	"Thursday");
				raw_source = raw_source.replace(/Viernes/g, "Friday");
				raw_source = raw_source.replace(/S.*bado/g, "Saturday");
				raw_source = raw_source.replace(/Domingo/g,	"Sunday");
				//alert (typeof raw_source);
				
				var week = raw_source.match('begins.*<');
				//var week = raw_source.match('a partir de.*<');
				//alert(week);
				
				raw_source = raw_source.replace(/(\r\n|\n|\r)/gm,"");
				raw_source = raw_source.replace(/\s+/g," ");
				raw_source = raw_source.replace(/&nbsp;/g, "")
				
				var theData = raw_source.match("cellInfo1aR.*region-");
				//alert (theData);
				
				
							
				var uniqueID = raw_source.match('headerName">.*> <input type="button" v');
				uniqueID = String(uniqueID);
				uniqueID = uniqueID.replace('headerName">','');
				uniqueID = uniqueID.replace(' <span id="logout"> <input type="button" v','');
				//alert (uniqueID);
				
				var hash = 0;
				for (i = 0; i < uniqueID.length; i++) {
					char = uniqueID.charCodeAt(i);
					hash = ((hash<<5)-hash)+char;
					hash = hash & hash; // Convert to 32bit integer
				}
				//var eventTitles = safari.extension.settings.eventTitles;
				//var server = safari.extension.settings.server;
				//var stf = safari.extension.settings.stf;
				//var etf = safari.extension.settings.etf;
				var location = loc;
				var current_date = new Date();
				var timezone = current_date.getTimezoneOffset() / 60;
				//var tzCheck = safari.extension.settings.tzCheck;
				//var offDay = safari.extension.settings.dayOff;
				//var offDayTitle = safari.extension.settings.dayOffTitle;
						
				var data = ("http://byronthegreat.com/kronical/createics.php"+"/?calendar=2"+week+"||||"+theData+"?eT="+"ï£¿%20[start]-[end]"+"?stf"+"3"+"?etf"+"3"+"?server"+"http://byronthegreat.com/kronical/createics.php"+"?location"+"Apple%20Store"+"?timezone"+timezone+"?hash="+hash+"?tzCheck="+"true"+"?offDay="+"false"+"?offDayTitle="+"Day%20Off"+"end|||");
			
                  
                  
                var server="http://byronthegreat.com/kronical/createics.php";
                var tzCheck="true"
                
                  //alert(stf);
                  //alert(etf);
                  //alert(server);
                  
				var data = ("http://byronthegreat.com/kronical/createics.php"+"/?calendar=2"+week+"||||"+theData+"?eT="+eventTitles+"?stf"+stf+"?etf"+etf+"?server"+server+"?location"+location+"?timezone"+timezone+"?hash="+hash+"?tzCheck="+tzCheck+"?offDay="+offDay+"?offDayTitle="+offDayTitle+"end|||");
			
			
				//http://byronthegreat.com/kronical/createics.php/?calendar=2begins%20Sep%2028,%202013%3C||||?eT=ï£¿%20[start]-[end]?stf3?etf3?serverhttp://byronthegreat.com/kronical/createics.php?locationApple%20Store?timezone5?hash=1623424429?tzCheck=true?offDay=false?offDayTitle=Day%20Offend|||
		
				//alert(data);
			
				document.location = data;
			}
			
			
			//safari.application.activeBrowserWindow.activeTab.url=data;
			
			
		})();
	}

})();